﻿using System;
using System.Web.Mvc;
using Domain.NHibernate.Models;
using Web.Logic.ViewModels;

namespace Web.Logic.Controllers
{
    public class DoerController : Controller
    {
        private readonly IUnitOfWorkFactory unitOfWorkFactory;

        public DoerController(IUnitOfWorkFactory unitOfWorkFactory)
        {
            this.unitOfWorkFactory = unitOfWorkFactory;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult FilterDoers()
        {
            return View(new FilterViewModel());
        }

        [HttpPost]
        public ActionResult FilterDoers(FilterViewModel viewModel)
        {
            using (IUnitOfWork unitOfWork = unitOfWorkFactory.Create())
            {
                var doers = new GetAllDoersQuery(unitOfWork.GetSessionAdapter()).Execute();

                var filterSet = viewModel.CreateFilterSet();

                var filteredDoerList = filterSet.Filter(doers);

                return View("FilterResult", filteredDoerList);
            }
        }

        public ActionResult ListDoers()
        {
            using (IUnitOfWork unitOfWork = unitOfWorkFactory.Create())
            {
                var doers = new GetAllDoersQuery(unitOfWork.GetSessionAdapter()).Execute();

                return View(doers);
            }
        }

    }
}